import ElementPlus from 'element-plus';
import type { App, Component, ComponentOptions } from 'vue';
import { createApp } from 'vue';

import { elementLocale, setupI18n } from '@/locales';

import { registerGlobComp } from '@/components/register';
import { setupGlobDirectives } from '@/directives';
import { setupRouter } from '@/router';
import { setupStore } from '@/store';

/**
 * 创建 vue 实例方法
 * @param comp 组件
 * @param options 组件参数
 * @returns app
 */
export const useCreateApp = (comp: Component, options?: ComponentOptions): App => {
  // 通过 createApp 创建的实例都需要再注册一遍全局组件
  const app = createApp(comp, options);

  // pinia store
  setupStore(app);

  // 注册全局组件
  registerGlobComp(app);

  // 自定义指令
  setupGlobDirectives(app);

  // 路由
  setupRouter(app);

  // 国际化
  setupI18n(app);

  // 注册ElementPlus
  app.use(ElementPlus, {
    locale: elementLocale(),
  });

  return app;
};
